package shapewar.net.io;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import shapewar.net.io.Tool;
import shapewar.net.seriz.CreateInfo;
import shapewar.net.seriz.DieInfo;
import shapewar.net.seriz.PosInfo;
import shapewar.net.seriz.Info;
import shapewar.net.seriz.MoveInfo;
import shapewar.net.seriz.NetConf;
import shapewar.net.seriz.ReliveInfo;
import shapewar.net.seriz.SynchInfo;
import shapewar.net.seriz.TextInfo;

public class OOS {
	ObjectOutputStream oos;
	OutputStream os;
	public OOS(OutputStream os) throws IOException {
		oos = new ObjectOutputStream(os);
		this.os = os;
	}
	
	public void writeObject(Info info) throws IOException{
		byte b[];
		switch(info.cmd){
		case NetConf.info_create:
			CreateInfo ci = (CreateInfo)info;
			b = Tool.combine(new int[]{
					ci.cmd,
					ci.id,
					(int)ci.x,
					(int)ci.y,
					(int)ci.target_x,
					(int)ci.target_y,
					ci.state,
					ci.protector_num,
					ci.killNum,
					ci.dieNum,
					(int)ci.map_width,
					(int)ci.map_height,
					(int)ci.speed * 100,
					ci.room_id
			}, ci.name + "," + ci.mac);
			os.write(b);
			break;
		case NetConf.info_move:
			MoveInfo mi = (MoveInfo)info;
			b = Tool.combine(new int[]{
					mi.cmd,
					mi.id,
					(int)mi.target_x,
					(int)mi.target_y,
					mi.blt_key,
					(int)mi.x,
					(int)mi.y
			}, null);
			os.write(b);
			break;
		case NetConf.info_flash:
			PosInfo fi = (PosInfo)info;
			b = Tool.combine(new int[]{
					fi.cmd,
					fi.id,
					(int)fi.x,
					(int)fi.y
			},null);
			os.write(b);
			break;
		case NetConf.info_die:
			DieInfo di = (DieInfo)info;
			b = Tool.combine(new int[]{
					di.cmd,
					di.id,
					di.killerId,
					di.killedDieNum,
					di.killerKillNum,
					di.ptrNum
			}, null);
			os.write(b);
			break;
		case NetConf.info_protectorMinus:
			DieInfo di1 = (DieInfo)info;
			b = Tool.combine(new int[]{
					di1.cmd,
					di1.id,
					di1.killerId,
					di1.bulletKey
			}, null);
			os.write(b);
			break;
		case NetConf.info_exit:
			b = Tool.combine(new int[]{
					info.cmd,
					info.id
			}, null);
			os.write(b);
			break;
		case NetConf.info_getId:
			b = Tool.combine(new int[]{
					info.cmd,
					info.id
			}, null);
			os.write(b);
			break;
		case NetConf.info_relive:
			ReliveInfo ri = (ReliveInfo)info;
			b = Tool.combine(new int[]{
					ri.cmd,
					ri.id,
					(int)ri.x,
					(int)ri.y,
			}, null);
			os.write(b);
			break;
		case NetConf.info_toast:
			TextInfo ti = (TextInfo)info;
			b = Tool.combine(new int[]{
					info.cmd,
					info.id
			}, ti.str);
			os.write(b);
			break;
		case NetConf.info_synch:
			SynchInfo si = (SynchInfo)info;
			int tmp[] = new int[si.length * si.size + 3];
			tmp[0] = si.cmd;
			tmp[1] = si.length;
			tmp[2] = si.size;
			int k = 3;
			for(int i=0;i<si.length;i++){
				for(int j=0;j<si.size;j++){
					tmp[k++] = si.infos[i][j];
				}
			}
			b = Tool.combine(tmp, null);
			os.write(b);
			break;
		default:
			b = Tool.combine(new int[]{info.cmd}, null);
			os.write(b);
			oos.writeObject(info);
			break;
		}
	}
	
	public void flush() throws IOException{
		//os.flush();
		os.flush();
		oos.flush();
	}
}
